{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 设一个2D矩阵某个值，如（i，j）点周围8个点的平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1\n",
      "1 2\n",
      "1 3\n",
      "1 4\n",
      "1 5\n",
      "1 6\n",
      "2 1\n",
      "2 2\n",
      "2 3\n",
      "2 4\n",
      "2 5\n",
      "2 6\n",
      "3 1\n",
      "3 2\n",
      "3 3\n",
      "3 4\n",
      "3 5\n",
      "3 6\n",
      "4 1\n",
      "4 2\n",
      "4 3\n",
      "4 4\n",
      "4 5\n",
      "4 6\n",
      "5 1\n",
      "5 2\n",
      "5 3\n",
      "5 4\n",
      "5 5\n",
      "5 6\n",
      "6 1\n",
      "6 2\n",
      "6 3\n",
      "6 4\n",
      "6 5\n",
      "6 6\n",
      "{(2, 2): [19, 50, 10, 25, 39, 10, 10, 28], (2, 3): [50, 10, 37, 61, 88, 10, 28, 32], (2, 4): [10, 37, 82, 39, 43, 28, 32, 45], (2, 5): [37, 82, 31, 88, 57, 32, 45, 92], (3, 2): [25, 61, 39, 10, 28, 33, 21, 87], (3, 3): [61, 39, 88, 10, 32, 21, 87, 10], (3, 4): [39, 88, 43, 28, 45, 87, 10, 54], (3, 5): [88, 43, 57, 32, 92, 10, 54, 48], (4, 2): [10, 10, 28, 33, 87, 10, 10, 44], (4, 3): [10, 28, 32, 21, 10, 10, 44, 66], (4, 4): [28, 32, 45, 87, 54, 44, 66, 83], (4, 5): [32, 45, 92, 10, 48, 66, 83, 32], (5, 2): [33, 21, 87, 10, 44, 75, 53, 49], (5, 3): [21, 87, 10, 10, 66, 53, 49, 10], (5, 4): [87, 10, 54, 44, 83, 49, 10, 11], (5, 5): [10, 54, 48, 66, 32, 10, 11, 92]}\n",
      "(3, 4) --> 49.25\n",
      "49.25\n"
     ]
    }
   ],
   "source": [
    "# %load ./homework3.py\n",
    "# 设置一个2D矩阵\n",
    "nums = [\n",
    "    [19, 50, 10, 37, 82, 31],\n",
    "    [25, 61, 39, 88, 43, 57],\n",
    "    [10, 10, 28, 32, 45, 92],\n",
    "    [33, 21, 87, 10, 54, 48],\n",
    "    [10, 10, 44, 66, 83, 32],\n",
    "    [75, 53, 49, 10, 11, 92]\n",
    "    ]\n",
    "\n",
    "# 这里map函数仅考虑周围有8个值的点\n",
    "def my_map(nums):\n",
    "    # 定义结果\n",
    "    result_path = {}\n",
    "    # 搜索集\n",
    "    each_path = {}\n",
    "    # 行数\n",
    "    row = len(nums)\n",
    "    # 列\n",
    "    col = len(nums[0])\n",
    "    for i in range(row):\n",
    "        for k in range(col):\n",
    "            each_path[(i+1,k+1)] = nums[i][k]\n",
    "            print(i+1,k+1)\n",
    "    for j in range(1,row-1):\n",
    "        for h in range(1,col-1):\n",
    "            result_path[(j+1,h+1)] = [\n",
    "                each_path[(j,h)],\n",
    "                each_path[(j,h+1)],\n",
    "                each_path[(j,h+2)],\n",
    "                each_path[(j+1,h)],\n",
    "                each_path[(j+1,h+2)],\n",
    "                each_path[(j+2,h)],\n",
    "                each_path[(j+2,h+1)],\n",
    "                each_path[(j+2,h+2)]\n",
    "                ]\n",
    "\n",
    "    return result_path\n",
    "\n",
    "path = my_map(nums)\n",
    "print(path)\n",
    "\n",
    "\n",
    "def my_reduce(tuple,path):\n",
    "    sum = 0\n",
    "    for i in path[tuple]:\n",
    "        sum = sum + i\n",
    "    return '{} --> {}'.format(tuple,sum/8)\n",
    "\n",
    "print(my_reduce((3,4),path))\n",
    "\n",
    "s =0\n",
    "for i in [39, 88, 43, 28, 45, 87, 10, 54]:\n",
    "    s += i\n",
    "print(s/8)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
